package com.db.person.controller;

import com.db.person.bean.Dept;
import com.db.person.bean.Notice;
import com.db.person.bean.User_inf;
import com.db.person.service.INoticeService;
import com.db.person.util.PageModel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.jws.WebParam;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class NoticeController {

    @Autowired
    private INoticeService noticeService;
    
    @RequestMapping("/notice/list")
    public String toList(PageModel pageModel , Model model, Integer pageIndex){

        //查询所有数据 进行分页
        PageHelper.startPage(pageModel.getPageIndex(),pageModel.getPageSize());
        List<Notice> all = noticeService.getAll();

        PageInfo<Notice> pageInfo = new PageInfo<>(all);

        List<Notice> list = pageInfo.getList();

        pageModel.setRecordCount((int)pageInfo.getTotal());

        model.addAttribute("list",list);
        model.addAttribute("pageModel",pageModel);

        model.addAttribute("hre","notice/list");
        return "notice/list";
    }

    @RequestMapping("/notice/toadd")
    public String toAdd(){

        return "notice/add";
    }

    @RequestMapping("/notice/add")
    public String add(Notice notice, HttpSession session){
        //我们要处理userid
        User_inf user = (User_inf) session.getAttribute("user_session");
        Integer userid = user.getId();
        notice.setUserId(userid);
        //调用service mapper db  入库
        noticeService.insert(notice);

        return "redirect:/notice/list";
    }

    @RequestMapping("/notice/toedit")
    public String toEdit(Integer id, Model model){
        //根据id查询对应的数据
        Notice notice = noticeService.getById(id);
        //把查询的数据传回给页面
        model.addAttribute("notice",notice);
        return "notice/edit";
    }

    @RequestMapping("/notice/edit")
    public String edit(Notice notice){

        //根据id 修改 title content
        noticeService.update(notice);

        return "redirect:/notice/list";

    }
}
